package com.example.anydo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.anydo.R;
import com.example.anydo.bean.SubTaskBean;
import com.example.anydo.bean.TaskBean;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class EditSubTaskAdapter extends RecyclerView.Adapter<EditSubTaskAdapter.ViewHolder> {
    private ArrayList<SubTaskBean.DataBean> dataBeanList;
    private Context context;

    public EditSubTaskAdapter(ArrayList<SubTaskBean.DataBean> dataBeanList, Context context){
        this.dataBeanList = dataBeanList;
        this.context = context;
    }


    public ArrayList<SubTaskBean.DataBean> getSubTasks(){
        return dataBeanList;
    }


    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.sub_new_todo_items,
                parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull final ViewHolder holder, final int position) {

        if(position < dataBeanList.size()){
            holder.im_sub.setVisibility(View.INVISIBLE);
            holder.delete.setVisibility(View.VISIBLE);
            holder.done.setVisibility(View.VISIBLE);
            holder.add.setVisibility(View.INVISIBLE);
            holder.title.setText(dataBeanList.get(position).getTitle());
            if ("是".equals(dataBeanList.get(position).getIfDone())){
                holder.done.setImageResource(R.drawable.done_true);
            }else {
                holder.done.setImageResource(R.drawable.done_false);
            }
        }else {
            holder.im_sub.setVisibility(View.VISIBLE);
            holder.delete.setVisibility(View.INVISIBLE);
            holder.done.setVisibility(View.INVISIBLE);
            holder.add.setVisibility(View.VISIBLE);
            holder.title.setText("");
        }

        holder.done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s_done = dataBeanList.get(position).getIfDone().equals("是") ? "否" : "是";
                dataBeanList.get(position).setIfDone(s_done);
                notifyDataSetChanged();
            }
        });

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataBeanList.remove(position);
                notifyDataSetChanged();
            }
        });

        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String temp = holder.title.getText().toString();
                if(!temp.equals("")){
                    dataBeanList.add(new SubTaskBean.DataBean(-1,-1,temp,"否"));
                }
                notifyDataSetChanged();
            }
        });
    }


    @Override
    public int getItemCount() {
        return dataBeanList.size()+1;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView done ;
        EditText title;
        ImageView delete ;
        Button add ;
        ImageView im_sub;
        public ViewHolder(@NonNull @NotNull View view) {
            super(view);
            done = view.findViewById(R.id.iv_if_done);
            title = view.findViewById(R.id.et_sub);
            delete = view.findViewById(R.id.iv_delete);
            add = view.findViewById(R.id.btn_add);
            im_sub = view.findViewById(R.id.im_sub);
        }
    }
}
